package com.common.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther WuJun.Zhang
 * @date 2022/5/17 下午4:29
 */
@Configuration
// @ConditionalOnProperty(prefix = "redis.son", name = "enable", havingValue = "true")
public class RedisSonConfig {

    /**
     * ping 连接间隔时间
     */
    private final static int PING_CONNECTION_INTERVAL = 6000;
    /**
     * 看门狗的锁续期时间
     */
    private final static long LOCK_WATCH_DOG_TIME_OUT = 15000L;
    /**
     * 重试次数
     */
    private final static int RETRY_ATTEMPTS = 5;
    /**
     * 重试时间间隔
     */
    private final static int RETRY_INTERVAL = 2000;
    /**
     * 超时时间
     */
    private final static int TIME_OUT = 5000;


    @Resource
    private RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        if (redisProperties.getCluster() != null) {
            //集群模式配置
            List<String> nodes = redisProperties.getCluster().getNodes();

            List<String> clusterNodes = new ArrayList<>();
            for (String node : nodes) {
                clusterNodes.add("redis://" + node);
            }
            ClusterServersConfig clusterServersConfig = config.useClusterServers()
                    .addNodeAddress(clusterNodes.toArray(new String[0]));
            if (StringUtils.hasText(redisProperties.getPassword())) {
                clusterServersConfig.setPassword(redisProperties.getPassword());
            }
            clusterServersConfig.setPingConnectionInterval(PING_CONNECTION_INTERVAL);
            clusterServersConfig.setRetryAttempts(RETRY_ATTEMPTS);
            clusterServersConfig.setRetryInterval(RETRY_INTERVAL);
            clusterServersConfig.setTimeout(TIME_OUT);
        } else {
            //单节点配置
            String address = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort();
            SingleServerConfig serverConfig = config.useSingleServer();
            serverConfig.setAddress(address);
            if (StringUtils.hasText(redisProperties.getPassword())) {
                serverConfig.setPassword(redisProperties.getPassword());
            }
            serverConfig.setPingConnectionInterval(PING_CONNECTION_INTERVAL);
            serverConfig.setRetryAttempts(RETRY_ATTEMPTS);
            serverConfig.setRetryInterval(RETRY_INTERVAL);
            serverConfig.setTimeout(TIME_OUT);
            serverConfig.setDatabase(redisProperties.getDatabase());
        }
        config.setLockWatchdogTimeout(LOCK_WATCH_DOG_TIME_OUT);
        return Redisson.create(config);
    }
}